package com.twu.biblioteca.core;

import com.twu.biblioteca.core.Book;
import com.twu.biblioteca.core.Movie;
import com.twu.biblioteca.core.User;
import com.twu.biblioteca.exceptions.BookNotFoundException;
import com.twu.biblioteca.exceptions.InvalidBookOperationException;
import com.twu.biblioteca.exceptions.InvalidMovieOperationException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cristianpinto on 2/17/14.
 */
public class Library {

    private List<Book> books;
    private List<Movie> movies;

    public Library(List<Book> books, List<Movie> movies) {
        this.books = books;
        this.movies = movies;
    }

    public void checkOutBook(String title, User user) throws InvalidBookOperationException {
        Book theBook = null;
        try {
            theBook = findBook(title);
        } catch (BookNotFoundException e) {
            System.out.println(e.getMessage());
        }
        if (!theBook.isAvailable())
            throw new InvalidBookOperationException("That book has already been checked out");
        theBook.setAvailable(false);
        theBook.setCheckedOutBy(user);
    }

    public Book findBook(String title) throws BookNotFoundException {
        for(Book book : books) {
            if (book.getTitle().equals(title))
                return book;
        }
        throw new BookNotFoundException("We can not find a book with that name");
    }

    public void checkInBook(String title) throws InvalidBookOperationException {
        Book theBook = null;
        try {
            theBook = findBook(title);
        } catch (BookNotFoundException e) {
            System.out.println(e.getMessage());
        }
        if (theBook.isAvailable())
            throw new InvalidBookOperationException("You are trying to return a book that is already in the library");
        theBook.setAvailable(true);
        theBook.setCheckedOutBy(null);
    }

    public List<Book> getAvailableBooks() {
        List<Book> availableBooks = new ArrayList<Book>();
        for (Book book : books) {
            if (book.isAvailable())
                availableBooks.add(book);
        }
        return availableBooks;
    }

    public Movie findMovie(String name) throws InvalidMovieOperationException {
        for(Movie movie : movies) {
            if (movie.getName().equals(name))
                return movie;
        }
        throw new InvalidMovieOperationException("Movie not found");

    }

    public void checkoutMovie(String name) throws InvalidMovieOperationException {
        Movie theMovie = null;
        try {
            theMovie = findMovie(name);
        } catch (InvalidMovieOperationException e) {
            System.out.println(e.getMessage());
        }
        if (!theMovie.getStatus())
            throw new InvalidMovieOperationException("Movie not found");
        theMovie.setStatus(false);
    }

    public List<Movie> getAvailableMovies() {
        List<Movie> availableMovies = new ArrayList<Movie>();
        for(Movie movie : movies) {
            if (movie.getStatus())
                availableMovies.add(movie);
        }
        return availableMovies;
    }
}
